export const SimpleTableMixins = {
    props: {
        staticParams: { // 静态参数
            type: Object,
            default: ()=>{
                return {}
            }
        }
    },
    data() {
        let that = this
        return {
            createLoadFlag: true,
            queryParam: {

            },
            baseParam: {
                sort: "createTime", // 默认排序字段
                order: "desc", // 默认排序方式
            },
            pageOption: {
                current: 1,
                total: 0,
                'page-size': 10,
                'page-size-opts': [10, 20, 50, 100],
                size: 'small',
                'show-total': true,
                'show-elevator': true,
                'show-sizer': true,
            },
            pageListener: {
                'on-change': that.changePage,
                'on-page-size-change': that.changePageSize,
            },
            dataSource: [],
            loading: false,
            total: 0,
            action: {
                listAction: null
            },
            selectRows: []
        }
    },
    methods: {
        onSelectChange(selection) {
            this.selectRows = selection
        },
        handleDeleteSingleById(ids) {
            this.$Modal.confirm({
                title: '确认',
                content: '是否确认删除当前数据?',
                onOk: () => {
                    this._deleteAction(ids)
                }
            });
        },
        handleDeleteBench() {

        },
        _deleteAction(ids) {
            if(typeof(this.action.deleteBenchAction) === 'function') {
                this.loading = true;
                this.action.deleteBenchAction(ids).then(res=>{
                    if (res.success) {
                        this.$Message.success('删除成功!');
                        this.reloadData();
                    }
                }).finally(()=>{
                    this.loading = false;
                })
            }else{
                console.error('SimpleTableMixins: action.listAction is required')
            }
        },
        handleAdd() {
            this.$refs.editModel.add({})
        },
        handleEdit(record) {
            this.$refs.editModel.edit(record)
        },
        reloadData(resetParamFlag=false) {
            if(resetParamFlag === true){
                this.queryParam = {}
            }
            this.loadData(1);
        },
        loadData(reloadFlag=false) {
            if(reloadFlag) {
                this.baseParam.pageNumber = 1;
                this.baseParam.pageSize = 10;
                this.pageOption.current = 1;
                this.pageOption.total = 0;
                this.pageOption['page-size'] = 10;
            }
            let params = this._getParams();
            this._loadDataAction(params);
        },
        _getParams() {
            let params = Object.assign({...this.staticParams}, {
                pageNumber: this.pageOption.current,
                pageSize: this.pageOption['page-size'],
            }, this.baseParam, this.queryParam);
            return  params
        },
        _loadDataAction(params) {
            if(typeof(this.action.listAction) === 'function') {
                this.selectRows = []
                this.loading = true;
                this.dataSource = []
                this.action.listAction(params).then(res=>{
                    if (res.success) {
                      this._parseListData(res)
                    }
                }).finally(()=>{
                    this.loading = false;
                })
            }else{
                console.error('SimpleTableMixins: action.listAction is required')
            }
        },
        _parseListData(res={}) {
            this.dataSource = res.result.records;
            this.pageOption.total = res.result.total;
        },
        // 分页 改变页码
        changePage(v) {
          this.baseParam.pageNumber = v;
          this.loadData();
        },
        // 分页 改变页数
        changePageSize(v) {
          this.baseParam.pageNumber = 1;
          this.baseParam.pageSize = v;
          this.loadData();
        },
    },
    mounted() {
        if(this.createLoadFlag) {
            this.reloadData()
        }
    }
}